local skel = fk.CreateSkill {
  name = "lb__qianyi",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable {
  ["lb__qianyi"] = "千役",
  [":lb__qianyi"] = "<b>锁定技</b>，你的第X个阶段结束后，若你手牌数较此阶段发动<a href=':lb__youyu'><font color='red'>〖游鱼〗</font></a>前变化过，你将手牌数调整至该阶段开始时（X为你回合开始时的手牌数）。",

  ["@lb__qianyi-turn"] = "千役",

  ["$lb__qianyi1"] = "愚者千面，游戏人间。",
  ["$lb__qianyi2"] = "你~会找到答案吗~",
}

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player
        and table.contains({ Player.Start, Player.Judge, Player.Draw, Player.Play, Player.Discard, Player.Finish }, player.phase) then
      local n = #player.room.logic:getEventsOfScope(GameEvent.Phase, 999, function(e)
        return e.data.who == player and
            table.contains({ Player.Start, Player.Judge, Player.Draw, Player.Play, Player.Discard, Player.Finish },
              e.data.phase)
      end, Player.HistoryTurn)
      if n == player:getMark("@lb__qianyi-turn") then
        local m = player:getTableMark("lb__youyu-turn")
        event:setCostData(self, { n = m[n] })
        return m[n] ~= player:getHandcardNum()
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = event:getCostData(self).n
    if n > player:getHandcardNum() then
      player:drawCards(n - player:getHandcardNum(), skel.name)
    else
      room:askToDiscard(target, {
        min_num = player:getHandcardNum() - n,
        max_num = player:getHandcardNum() - n,
        include_equip = false,
        skill_name = skel.name,
        cancelable = false,
      })
    end
  end,
})

skel:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@lb__qianyi-turn", player:getHandcardNum())
  end,
})

return skel
